package com.xuelang.pipeline.app.pipeline.converter;

import com.xuelang.pipeline.app.resource.util.TektonUtil;
import com.xuelang.pipeline.client.pipeline.dto.PipelineDTO;
import com.xuelang.pipeline.client.pipeline.enums.PipelineInstanceEnum;
import com.xuelang.pipeline.client.pipeline.enums.TaskInstanceEnum;
import com.xuelang.pipeline.client.task.dto.PipelineConfigTaskDTO;
import com.xuelang.pipeline.domain.pipeline.PipelineInstance;
import com.xuelang.pipeline.domain.task.TaskInstance;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.Date;

/**
 * @author liuyuan.ly
 * @since 2024/7/1 11:18
 */
@Mapper
public interface ConfigToInstanceConverter {

    ConfigToInstanceConverter INSTANCE = Mappers.getMapper(ConfigToInstanceConverter.class);


    default PipelineInstance toPipelineInstance(PipelineDTO target) {

        String uniqueCode = target.getComponentLabel() + "-" + TektonUtil.getTimeString();
        PipelineInstance instance = PipelineInstance.builder()
                .pipelineCode(target.getCode())
                .instanceCode(uniqueCode)
                //根据组件label（唯一）+ 环境uuid（唯一）生成构建号target.getComponentLabel() + "-" + target.getEnvironmentUuid()
//                .buildNumber()
                .applicationCode(target.getApplicationCode())
                .componentLabel(target.getComponentLabel())
                .pipelineName(target.getName())
                .startTime(new Date())
                .pipelineStatus(PipelineInstanceEnum.running.getKey())
                //mapper里统一插入
//                .creatorId(TracingUtils.getUserId())
                .createTime(new Date())
                .remark(target.getRemark())
                .build();
        return instance;
    }


    default TaskInstance toTaskInstance(PipelineConfigTaskDTO target) {
        return TaskInstance.builder()
//                .pipelineInstanceCode()
//                .code(target.getCode())    // 后续操作唯一凭证
                .name(target.getName())
                .taskUuid(target.getUuid())
                .taskParentUuid(target.getParentUuid())
                .params(target.getParams())
                .sortIndex(target.getSortIndex())
                .taskStatus(TaskInstanceEnum.notStart.getKey())
                //mapper里统一插入
//                .creatorId(TracingUtils.getUserId())
                .createTime(new Date())
                .build();
    }

}
